package com.cskaoyan.service;

import com.cskaoyan.bean.MarketSearchHistory;
import com.cskaoyan.bean.MarketSearchHistoryExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author Ramsey
 * @date 2023年05月28日 15:25
 */
public class AdminHistoryServiceimpl implements AdminHistoryService {

    @Override
    public CommonData query(Integer page, Integer limit, String sort, String order) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketSearchHistoryMapper mapper = sqlSession.getMapper(MarketSearchHistoryMapper.class);
        PageHelper.startPage(page, limit);
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        marketSearchHistoryExample.setOrderByClause(sort + " " + order);
        List<MarketSearchHistory> marketSearchHistories = mapper.selectByExample(marketSearchHistoryExample);
        PageInfo<MarketSearchHistory> marketSearchHistoryPageInfo = new PageInfo<>(marketSearchHistories);
        return CommonData.data(marketSearchHistoryPageInfo);
    }

    @Override
    public List<MarketSearchHistory> selectByKeyword(String keyword) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketSearchHistoryMapper mapper = sqlSession.getMapper(MarketSearchHistoryMapper.class);
        // MarketSearchHistory marketSearchHistory = mapper.selectByPrimaryKey(1);
        // System.out.println(marketSearchHistory);
        List<MarketSearchHistory> marketSearchHistories = mapper.selectByKeyword(keyword);
        for (MarketSearchHistory marketSearchHistory : marketSearchHistories) {
            System.out.println(marketSearchHistory);
        }
        return marketSearchHistories;
    }

    @Override
    public List<MarketSearchHistory> selectByUserId(Integer userId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketSearchHistoryMapper mapper = sqlSession.getMapper(MarketSearchHistoryMapper.class);
        List<MarketSearchHistory> marketSearchHistories = mapper.selectByUserId(userId);
        for (MarketSearchHistory marketSearchHistory : marketSearchHistories) {
            System.out.println(marketSearchHistory);
        }
        return marketSearchHistories;
    }
}
